package com.happyscan138.makefun;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.happyscan138.makefun.Urls.Urls;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/9.
 */

public class more_set extends AppCompatActivity implements View.OnClickListener{
    Button buttonModify,buttonForget,ButtonLogOff;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private String accessy_ley;
    private ImageView back;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set);
        preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        accessy_ley=preferences.getString("UserId","");
        initView();
    }
    public void initView(){
        back=(ImageView)findViewById(R.id.set_back);
        back.setOnClickListener(this);
        buttonModify=(Button) findViewById(R.id.set_modify_text);
        buttonForget=(Button)findViewById(R.id.set_forget_text);
        ButtonLogOff=(Button)findViewById(R.id.exit) ;
        buttonForget.setOnClickListener(this);
        buttonModify.setOnClickListener(this);
        ButtonLogOff.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        client=new AsyncHttpClient();
        params=new RequestParams();
        params.put("access_key",accessy_ley);
        switch (view.getId()){
            case R.id.set_back:
                finish();
                break;
            case R.id.set_modify_text:
                startActivity(new Intent(this,ModifyPassword.class));
                break;

            case R.id.set_forget_text:
                startActivity(new Intent(this,forgetPassword.class));
                break;
            case R.id.exit:
                log.e("access_key",accessy_ley);
                RequestHandle handle=client.post(Urls.LogOff,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();
                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            String msg=jsonObject.getString("msg");
                            Toast.makeText(more_set.this,msg,Toast.LENGTH_SHORT).show();
                            int code=jsonObject.getInt("code");
                            if (code == 1) {
                                startActivity(new Intent(more_set.this,Land.class));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
        }
    }
}
